home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Online / opennap / list.c < prev    next >
C/C++ Source or Header  |  2001-06-08  |  2KB  |  136 lines

  1. /* Copyright (C) 2000-1 drscholl@users.sourceforge.net
  2.    This is free software distributed under the terms of the
  3.    GNU Public License.  See the file COPYING for details.
  4.  
  5.    $Id: list.c,v 1.14 2001/02/15 08:39:45 drscholl Exp $ */
  6.  
  7. #include <stdlib.h>
  8. #include "list.h"
  9. #include "debug.h"
  10.  
  11. LIST   *
  12. list_new (void *p)
  13. {
  14.     LIST   *list = CALLOC (1, sizeof (LIST));
  15.  
  16.     if (list)
  17.     list->data = p;
  18.     return list;
  19. }
  20.  
  21. /* remove the element matching `data' from the list */
  22. LIST   *
  23. list_delete (LIST * list, void *data)
  24. {
  25.     LIST  **ptr, *tmp;
  26.  
  27.     ASSERT (list != 0);
  28.     ASSERT (data != 0);
  29.     for (ptr = &list; *ptr; ptr = &(*ptr)->next)
  30.     {
  31.     ASSERT (VALID_LEN (*ptr, sizeof (LIST)));
  32.     if ((*ptr)->data == data)
  33.     {
  34.         tmp = *ptr;
  35.         *ptr = (*ptr)->next;
  36.         FREE (tmp);
  37.         break;
  38.     }
  39.     }
  40.     return list;
  41. }
  42.  
  43. LIST   *
  44. list_append (LIST * l, LIST * b)
  45. {
  46.     LIST  **r = &l;
  47.  
  48.     while (*r)
  49.     {
  50.     ASSERT (VALID_LEN (*r, sizeof (LIST)));
  51.     r = &(*r)->next;
  52.     }
  53.     *r = b;
  54.     return l;
  55. }
  56.  
  57. LIST   *
  58. list_append_data (LIST * l, void *d)
  59. {
  60.     LIST   *list;
  61.  
  62.     ASSERT (d != 0);
  63.     LIST_NEW (list, d);
  64.     return (list_append (l, list));
  65. }
  66.  
  67. void
  68. list_free (LIST * l, list_destroy_t cb)
  69. {
  70.     LIST   *t;
  71.  
  72.     while (l)
  73.     {
  74.     ASSERT (VALID_LEN (l, sizeof (LIST)));
  75.     t = l;
  76.     l = l->next;
  77.     if (cb)
  78.         cb (t->data);
  79.     FREE (t);
  80.     }
  81. }
  82.  
  83. int
  84. list_count (LIST * list)
  85. {
  86.     int     count = 0;
  87.  
  88.     for (; list; list = list->next)
  89.     {
  90.     ASSERT (VALID_LEN (list, sizeof (LIST)));
  91.     count++;
  92.     }
  93.     return count;
  94. }
  95.  
  96. LIST   *
  97. list_find (LIST * list, void *data)
  98. {
  99.     for (; list; list = list->next)
  100.     {
  101.     ASSERT (VALID_LEN (list, sizeof (LIST)));
  102.     if (list->data == data)
  103.         return list;
  104.     }
  105.     return 0;
  106. }
  107.  
  108. #if DEBUG
  109. int
  110. list_validate (LIST * list)
  111. {
  112.     for (; list; list = list->next)
  113.     {
  114.     ASSERT_RETURN_IF_FAIL (VALID_LEN (list, sizeof (LIST)), 0);
  115.     }
  116.     return 1;
  117. }
  118. #endif
  119.  
  120. LIST   *
  121. list_push (LIST * head, LIST * elem)
  122. {
  123.     elem->next = head;
  124.     return elem;
  125. }
  126.  
  127. void
  128. list_foreach (LIST * list, list_callback_t func, void *arg)
  129. {
  130.     while (list)
  131.     {
  132.     func (list->data, arg);
  133.     list = list->next;
  134.     }
  135. }
  136.